package com.dubbo.snail.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.dubbo.snail.httpService.NacosProviderFeign;
import com.dubbo.snail.lb.LoadBalancer;
import com.dubbo.snail.lb.MyCustomLoadBalancer;
import com.nacos.snail.api.service.IHelloService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @Author xueguoping
 * @Date 2021/5/8 10:21
 */
@RefreshScope
@RestController
//@DefaultProperties(defaultFallback = "gloab_balanceOpenFeignTimeOut_handler")
public class HelloController {

	//@Reference
	private IHelloService helloService;

	@Autowired
	private RestTemplate restTemplate;

	@Value("${info}")
	private String info;

	/*@Value("${ainfo}")
	private String ainfo;*/

	@Autowired
	private DiscoveryClient discoveryClient;

	@Autowired
	private LoadBalancer loadBalancer;

	@Autowired
	private NacosProviderFeign nacosProviderFeign;

	//@SentinelResource(value = "sayHello",blockHandler = "blockHandlerHello")
	@GetMapping("/say")
	public String sayHello() throws InterruptedException {
		//return "info:"+info+" ainfo:"+ainfo;
		TimeUnit.SECONDS.sleep(5);
		return info;
		//return helloService.sayHello("snail");
	}

	/*public String blockHandlerHello(BlockException e){
		return "被限流了";
	}*/

	@GetMapping("/clean/{id}")
	public String clean(@PathVariable("id")int id){
		return "Hello clean "+id;
	}
	String url = "http://spring-cloud-nacos-sample/getProvider";
	@GetMapping("/balance")
	public String balance(){
		Object forObject = restTemplate.getForObject(url, Object.class);
		ResponseEntity<Object> forEntity = restTemplate.getForEntity(url, Object.class);
		return JSONObject.toJSONString(forEntity);
	}

	@GetMapping("/balance2")
	public String balance2(){
		List<ServiceInstance> instances = discoveryClient.getInstances("spring-cloud-nacos-sample");
		if(CollectionUtils.isEmpty(instances)){
			return null;
		}

		ServiceInstance instance = loadBalancer.instance(instances);
		String url="http://"+instance.getHost()+":"+instance.getPort()+"/getProvider";
		Object forObject = restTemplate.getForObject(url, Object.class);
		return JSONObject.toJSONString(forObject);
	}

	//@HystrixCommand
	@GetMapping("/balanceOpenFeign")
	public String balanceOpenFeign(){
		//int age = 10/0;
		String provider = nacosProviderFeign.getProvider();
		return provider;
	}

	/*@HystrixCommand(fallbackMethod = "balanceOpenFeignTimeOut_handler",commandProperties ={
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
	})*/
	@GetMapping("/balanceOpenFeignTimeOut")
	public String balanceOpenFeignTimeOut(){
		//int age = 10/0;
		String provider = nacosProviderFeign.getProviderTimeOut();
		return provider;
	}

	/*public String balanceOpenFeignTimeOut_handler(){
		return Thread.currentThread().getName()+"  balanceOpenFeignTimeOut_handler fallback";
	}

	public String gloab_balanceOpenFeignTimeOut_handler(){
		return Thread.currentThread().getName()+"  gloab_balanceOpenFeignTimeOut_handler";
	}*/

}
